﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;

namespace BoriPeter.FitnessJournal8.Models
{
    public class Food : ModelBase, IIdentifiable, IMergeable<Food>
    {
        #region Id
        private int id;
        public int Id
        {
            get { return id; }
            set 
            { 
                id = value;
                NotifyOfPropertyChange("Id");
            }
        }
        #endregion

        #region Name
        private string name;
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                NotifyOfPropertyChange("Name");
            }
        }
        #endregion

        #region Description
        private string description;
        public string Description
        {
            get
            {
                return this.description;
            }

            set
            {
                this.description = value;
                NotifyOfPropertyChange("Description");
            }
        }
        #endregion

        #region Brand
        private string brand;
        public string Brand
        {
            get
            {
                return this.brand;
            }

            set
            {
                this.brand = value;
                NotifyOfPropertyChange("Brand");
            }
        }
        #endregion

        #region Type
        private string type;
        public string Type
        {
            get
            {
                return this.type;
            }

            set
            {
                this.type = value;
                NotifyOfPropertyChange("Type");
            }
        }
        #endregion

        #region ImageName
        private string imageName;
        public string ImageName
        {
            get
            {
                return this.imageName;
            }

            set
            {
                this.imageName = value;
                NotifyOfPropertyChange("ImageUrl");
                image = null;
                NotifyOfPropertyChange("Image");
                NotifyOfPropertyChange("FoodImageVisibility");
            }
        }
        #endregion

        #region ImageUrl
        public string ImageUrl
        {
            get 
            {
                string fullUrl = @"http://images.fatsecret.com/food/" + imageName + @".jpg";
                return fullUrl; 
            }
        }
        #endregion

        #region Image
        private BitmapImage image;
        public BitmapImage Image
        {
            get
            {
                if ((imageName != null) && (image == null))
                {
                    image = new BitmapImage(new Uri(ImageUrl));
                }
                return image;
            }
        }
        #endregion

        #region FoodImageVisibility
        public Visibility FoodImageVisibility
        {
            get
            {
                if (imageName == null)
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;
            }
        }
        #endregion

        #region Servings
        private ObservableCollection<Serving> servings;
        public ObservableCollection<Serving> Servings
        {
            get
            {
                return this.servings;
            }

            set
            {
                this.servings = value;
                NotifyOfPropertyChange("Servings");
            }
        }
        #endregion

        #region SelectedServing
        private Serving selectedServing;
        public Serving SelectedServing
        {
            get
            {
                if ((selectedServing == null) && (servings != null))
                    selectedServing = servings[0];
                return this.selectedServing;
            }

            set
            {
                this.selectedServing = value;
                NotifyOfPropertyChange("SelectedServing");
                NotifyOfPropertyChange("SingleServingVisibility");
                NotifyOfPropertyChange("MultipleServingVisibility");
            }
        }
        public Visibility SingleServingVisibility
        {
            get
            {
                if (servings == null)
                    return Visibility.Collapsed;
                else if (servings.Count < 2)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }
        public Visibility MultipleServingVisibility
        {
            get
            {
                if (servings == null)
                    return Visibility.Collapsed;
                else if (servings.Count > 1)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }
        #endregion

        #region LastAccessedDate
        private DateTime lastAccessedDate;
        public DateTime LastAccessedDate
        {
            get
            {
                return this. lastAccessedDate;
            }

            set
            {
                this. lastAccessedDate = value;
                NotifyOfPropertyChange("LastAccessedDate");
            }
        }
        #endregion

        #region VisitedFrequency
        private int visitedFrequency;
        public int VisitedFrequency
        {
            get
            {
                return this.visitedFrequency;
            }

            set
            {
                this.visitedFrequency = value;
                NotifyOfPropertyChange("VisitedFrequency");
            }
        }
        #endregion

        #region Entries
        private ObservableCollection<FoodEntry> entries;
        public ObservableCollection<FoodEntry> Entries
        {
            get
            {
                return this.entries;
            }

            set
            {
                this.entries = value;
                NotifyOfPropertyChange("Entries");
            }
        }
        #endregion

        #region NewEntry
        private FoodEntry newEntry;
        public FoodEntry NewEntry
        {
            get
            {
                return this.newEntry;
            }

            set
            {
                this.newEntry = value;
                NotifyOfPropertyChange("NewEntry");
            }
        }
        #endregion

        #region Favorite
        private bool favorite;
        public bool Favorite
        {
            get
            {
                return this.favorite;
            }

            set
            {
                this.favorite = value;
                NotifyOfPropertyChange("Favorite");
                NotifyOfPropertyChange("NotFavorite");
            }
        }

        private bool favoriteNotStored;
        public bool FavoriteNotStored
        {
            get
            {
                return this.favoriteNotStored;
            }

            set
            {
                this.favoriteNotStored = value;
                NotifyOfPropertyChange("FavoriteNotStored");
            }
        }

        public bool NotFavorite
        {
            get
            {
                return !Favorite;
            }
        }
        #endregion

        #region Merge()
        public Food Merge(Food _food)
        {
            return MergeObjects.Merge<Food>(this, _food);
        }
        #endregion

        #region GetIdentifier()
        public int GetIdentifier()
        {
            return Id;
        }
        #endregion
    }
}
